package com.sudao.android.candle;

import android.content.ComponentName;
import android.content.Context;
import android.content.ContentValues;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.util.Log;

import java.lang.Integer;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;

public class PlayList extends SQLiteOpenHelper {

    private static final String LOG_TAG = PlayList.class.getSimpleName();
    private static final String DATABASE_NAME = "songs.db";
    private static final int DATABASE_VERSION = 1;

    private static final String TABLE_SONGS = "songs";
    private static final String SONG = "song";

    public PlayList(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        try {
            db.execSQL("CREATE TABLE songs (" +
                    "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    "song TEXT NOT NULL" +
                    ");");
        } catch (SQLException e) {
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
        if (oldVersion != DATABASE_VERSION) {
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_SONGS);
            onCreate(db);
        }
    }

	public void updateSongs(ArrayList<String> songs) {
        deleteSongs();
		insertSongs(songs);
    }

    public ArrayList<String> getSongs() {
		if (!FileBrowser.isSdcardMounted())
			return null;
		ArrayList<String> songs = null;
        SQLiteDatabase db = getWritableDatabase();
        String cols[] = { SONG };
        Cursor cursor = db.query(TABLE_SONGS, cols, null, null, null, null, null); 
        try {
            int rowNum = cursor.getCount();
            if (rowNum <= 0) {
                return null;
            }
			songs = new ArrayList<String>();
            int i = 0;
            while (cursor.moveToNext()) {
                String song = cursor.getString(0);
				songs.add(song);
                ++i;
            }
        } finally {
            db.close();
            cursor.close();
        }
        return songs;
    }

	
    private boolean insertSongs(ArrayList<String> songs) {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues value = new ContentValues();

		db.beginTransaction();
	 	try {
			int numValues = songs.size();
			for (int i = 0; i < numValues; i++) {
				value.put(SONG, songs.get(i));
				if (db.insert(TABLE_SONGS, null, value) < 0)
					return false;
			}
			db.setTransactionSuccessful();
		} finally {
			db.endTransaction();
		}
		db.close();
        return true;
    }

    private boolean deleteSongs() {
        SQLiteDatabase db = getWritableDatabase(); 
        boolean ret = db.delete(TABLE_SONGS, null, null) > 0;
        return ret;
    }
}

